<!--
   Copyright 2024 The Google Research Authors.
  
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
  
       http://www.apache.org/licenses/LICENSE-2.0
  
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->

<!DOCTYPE html>
<html>
<head>
<title>Click Button Sequence Click Option Task</title>
<!-- stylesheets -->
<link rel="stylesheet" type="text/css" href="../core/core.css">
<link rel="stylesheet" href="../core/jquery-ui/jquery-ui.min.css">
<style>
  input { width: 20px; }
  #subbtn1 { width: 40px; height: 40px; }
  #subbtn2 { width: 40px; height: 40px; }
  #wrap,
  #sync-task-cover {
    height: 300px;
    width: 160px;
  }

  #click-canvas,
  #reward-display {
    left: 165px;
  }

  #query {
    background-color: #FF0;
    font-size: 10px;
    height: 54px;
    padding: 3px;
  }
</style>

<!-- JS -->
<script src="../core/core.js"></script>
<script src="../core/d3.v3.min.js"></script>
<script src="../common/ui_utils.js"></script>
<script src="../core/jquery-ui/external/jquery/jquery.js"></script>
<script src="../core/jquery-ui/jquery-ui.min.js"></script>

<script>
core.EPISODE_MAX_TIME = 2000000;  // 2000 seconds

var createElements = function(div){
  var n = core.randi(2, 7);
  var clickNames = [];
  for(var i=0;i<n;i++) {
    var chname = ui_utils.generateString(2,8);
    var label = div.append('label')
    label.append('input').attr('type', 'radio').attr('id', 'ch'+i).attr('name', 'radio');
    label[0][0].innerHTML += chname;
    div.append('br');
    clickNames.push(chname);
  }
  var ix = core.randi(0, n);
  var qstr = clickNames[ix];
  return {query: qstr, index: ix};
}

var genProblem = function() {
  // click-button-sequence
  var button_state = -1.0
  // var L = core.randi(0, 118); var U = core.randi(0, 118) + 50;
  var L = core.randi(0, 118); var U = core.randi(0, 38) + 60 + 120;
  d3.select('#subbtn1').attr('style', 'position:absolute; left:'+L+'px; top:'+U+'px');
  d3.select('#subbtn1').on('click', function(){
    if (button_state == -1.0) {
      button_state = 1.0
    } else {
      button_state = -2.0
    }
  });

  // var L = core.randi(0, 118); var U = core.randi(0, 118) + 50;
  var L = core.randi(0, 118); var U = core.randi(0, 38) + 60 + 120;
  d3.select('#subbtn2').attr('style', 'position:absolute; left:'+L+'px; top:'+U+'px');
  d3.select('#subbtn2').on('click', function(){
    if (button_state == 1.0) {
      button_state = 2.0
    } else {
      button_state = -2.0
    }
  });

  // click-option
  var div = d3.select('#boxes');
  div.html('');

  var correctElement = createElements(div);

  d3.select('#query').html('Click button ONE, then click button TWO, and then select ' + correctElement.query + ' and click Submit.');
  // d3.select('#query').html('Select ' + correctElement.query + ' and click Submit, after clicking button ONE, then clicking button TWO.');

  d3.select('#subbtn3').on('click', function(){
    var r = d3.select('#ch'+correctElement.index)[0][0].checked ? 1.0 : -1.0;
    if (button_state == 2.0) {
      core.endEpisode(r, r > 0);
    } else {
      core.endEpisode(-1.0);
    }
  });
}

window.onload = function() {
  core.startEpisode();
}
</script>
</head>
<!-- Base HTML -->
<body>
<div id="wrap">
  <div id="query"></div>
  <div id="area">
    <button id="subbtn1">ONE</button>
    <button id="subbtn2">TWO</button>
    <div id="boxes"></div>
    <br>
    <button id="subbtn3" class="secondary-action">Submit</button>
  </div>
</div>
</body>
</html>
